home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / tptc17tc.zip / MINICRT.PAS < prev    next >
Pascal/Delphi Source File  |  1988-03-25  |  7KB  |  315 lines

  1.  
  2. (*
  3.  * MiniCrt - simplified version of Borland's CRT unit.
  4.  * Does not EVER do direct video.  The standard crt unit
  5.  * locks up multi-taskers with its direct video checking before
  6.  * the user program can turn it off.
  7.  *
  8.  * Samuel H. Smith, 20-dec-87
  9.  *
  10.  *)
  11.  
  12. {$i prodef.inc}
  13.  
  14. unit MiniCrt;
  15.  
  16. interface
  17.  
  18.    uses
  19.       Dos;
  20.  
  21.    var
  22.       stdout:  text;  {output through dos for ANSI compatibility}
  23.  
  24.    function KeyPressed: Boolean;
  25.    function ReadKey: Char;
  26.  
  27.    procedure Window(X1,Y1,X2,Y2: Byte);  {only partial support}
  28.  
  29.    procedure GotoXY(X,Y: Byte);
  30.    function WhereX: Byte;
  31.    function WhereY: Byte;
  32.  
  33.    procedure ClrScr;
  34.    procedure ClrEol;
  35.  
  36.    procedure NormalVideo;
  37.    procedure ReverseVideo;
  38.    procedure BlinkVideo;
  39.  
  40.  
  41.    (* -------------------------------------------------------- *)
  42.    procedure ScrollUp;
  43.    {$F+} function ConFlush(var F: TextRec): integer; {$F-}
  44.    {$F+} function ConOutput(var F: TextRec): integer; {$F-}
  45.    {$F+} function ConOpen(var F: TextRec): Integer; {$F-}
  46.  
  47.  
  48. (* -------------------------------------------------------- *)
  49. implementation
  50.  
  51.    const
  52.       window_y1  : byte = 1;
  53.       window_y2  : byte = 25;
  54.       TextAttr   : byte = $0f;
  55.       key_pending: char = #0;
  56.  
  57.  
  58.    (* -------------------------------------------------------- *)
  59.    function ReadKey: Char;
  60.    var
  61.       reg: registers;
  62.    begin
  63.       if key_pending <> #0 then
  64.       begin
  65.          ReadKey := key_pending;
  66.          key_pending := #0;
  67.          exit;
  68.       end;
  69.  
  70.       reg.ax := $0100;   {check for character}
  71.       intr($16,reg);
  72.       if (reg.flags and FZero) = 0 then
  73.       begin
  74.          reg.ax := $0000;   {wait for character}
  75.          intr($16,reg);
  76.          if reg.al = 0 then
  77.             key_pending := chr(reg.ah);
  78.       end
  79.       else
  80.  
  81.       begin
  82.          reg.ax := $0700;   {direct console input}
  83.          msdos(reg);
  84.       end;
  85.  
  86.       ReadKey := chr(reg.al);
  87.    end;
  88.  
  89.  
  90.    (* -------------------------------------------------------- *)
  91.    function KeyPressed: Boolean;
  92.    var
  93.       reg: registers;
  94.    begin
  95.       reg.ax := $0b00;   {ConInputStatus}
  96.       msdos(reg);
  97.       KeyPressed := (reg.al = $FF) or (key_pending <> #0);
  98.    end;
  99.  
  100.  
  101.    (* -------------------------------------------------------- *)
  102.    procedure Window(X1,Y1,X2,Y2: Byte);
  103.    begin
  104.       window_y1 := y1;
  105.       window_y2 := y2;
  106.    end;
  107.  
  108.  
  109.    (* -------------------------------------------------------- *)
  110.    procedure GotoXY(X,Y: Byte);
  111.    var
  112.       reg: registers;
  113.    begin
  114.       reg.ah := 2;   {set cursor position}
  115.       reg.bh := 0;   {page}
  116.       reg.dh := y-1;
  117.       reg.dl := x-1;
  118.       intr($10,reg);
  119.    end;
  120.  
  121.  
  122.    (* -------------------------------------------------------- *)
  123.    function WhereX: Byte;
  124.    var
  125.       reg: registers;
  126.    begin
  127.       reg.ah := 3;
  128.       reg.bh := 0;
  129.       intr($10,reg);
  130.       WhereX := reg.dl+1;
  131.    end;
  132.  
  133.    function WhereY: Byte;
  134.    var
  135.       reg: registers;
  136.    begin
  137.       reg.ah := 3;
  138.       reg.bh := 0;
  139.       intr($10,reg);
  140.       WhereY := reg.dh+1;
  141.    end;
  142.  
  143.  
  144.    (* -------------------------------------------------------- *)
  145.    procedure ClrScr;
  146.    var
  147.       reg: registers;
  148.    begin
  149.       reg.ax := $0600;  {scroll up, blank window}
  150.       reg.cx := 0;      {upper left}
  151.       reg.dx := $194F;  {line 24, col 79}
  152.       reg.bh := TextAttr;
  153.       intr($10,reg);
  154.       GotoXY(1,1);
  155.    end;
  156.  
  157.  
  158.    (* -------------------------------------------------------- *)
  159.    procedure ClrEol;
  160.    var
  161.       reg: registers;
  162.    begin
  163.       reg.ax := $0600;  {scroll up, blank window}
  164.       reg.ch := wherey-1;
  165.       reg.cl := wherex-1;
  166.       reg.dh := reg.ch;
  167.       reg.dl := 79; {lower column}
  168.       reg.bh := TextAttr;
  169.       intr($10,reg);
  170.    end;
  171.  
  172.  
  173.    (* -------------------------------------------------------- *)
  174.    procedure NormalVideo;
  175.    begin
  176.       TextAttr := $0f;
  177.    end;
  178.  
  179.    procedure ReverseVideo;
  180.    begin
  181.       TextAttr := $70;
  182.    end;
  183.  
  184.    procedure BlinkVideo;
  185.    begin
  186.       TextAttr := $F0;
  187.    end;
  188.  
  189.  
  190.    (* -------------------------------------------------------- *)
  191.    procedure ScrollUp;
  192.    var
  193.       reg: registers;
  194.    begin
  195.       reg.ah := 6;            {scroll up}
  196.       reg.al := 1;            {lines}
  197.       reg.cx := 0;            {upper left}
  198.       reg.dh := window_y2-1;  {lower line}
  199.       reg.dl := 79;           {lower column}
  200.       reg.bh := TextAttr;
  201.       intr($10,reg);
  202.    end;
  203.  
  204.  
  205.    (* -------------------------------------------------------- *)
  206.    {$F+} function ConFlush(var F: TextRec): integer; {$F-}
  207.    var
  208.       P:   Word;
  209.       reg: registers;
  210.       x,y: byte;
  211.  
  212.    begin
  213.       {get present cursor position}
  214.       reg.ah := 3;
  215.       reg.bh := 0;
  216.       intr($10,reg);
  217.       y := reg.dh+1;
  218.       x := reg.dl+1;
  219.  
  220.       {process each character in the buffer}
  221.       P := 0;
  222.       while P < F.BufPos do
  223.       begin
  224.          reg.al := ord(F.BufPtr^[P]);
  225.  
  226.          case reg.al of
  227.              7:  write(stdout,^G);
  228.  
  229.             10:  if y >= window_y2 then   {scroll when needed}
  230.                     ScrollUp
  231.                  else
  232.                     inc(y);
  233.  
  234.             13:  x := 1;
  235.  
  236.             else 
  237.             begin
  238.                  reg.ah := 9;  {display character with TextAttr}
  239.                  reg.bx := 0;  {... does not move the cursor}
  240.                  reg.cx := 1;
  241.                  reg.bl := TextAttr;
  242.                  intr($10,reg);
  243.  
  244.                  if x = 80 then   {line wrap?}
  245.                  begin
  246.                     x := 1;
  247.                     if y >= window_y2 then   {scroll during wrap?}
  248.                        ScrollUp
  249.                     else
  250.                        inc(y);
  251.                  end
  252.                  else
  253.                     inc(x);
  254.             end;
  255.          end;
  256.  
  257.          {position physical cursor}
  258.          reg.ah := 2;   {set cursor position}
  259.          reg.bh := 0;   {page}
  260.          reg.dh := y-1;
  261.          reg.dl := x-1;
  262.          intr($10,reg);
  263.  
  264.          inc(P);
  265.       end;
  266.  
  267.       F.BufPos:=0;
  268.       ConFlush := 0;
  269.    end;
  270.  
  271.  
  272.    {$F+} function ConOutput(var F: TextRec): integer; {$F-}
  273.    begin
  274.       ConOutput := ConFlush(F);
  275.    end;
  276.  
  277.  
  278.    {$F+} function ConOpen(var F: TextRec): Integer; {$F-}
  279.    begin
  280.       F.InOutFunc := @ConOutput;
  281.       F.FlushFunc := @ConFlush;
  282.       F.CloseFunc := @ConFlush;
  283.       F.BufPos := 0;
  284.       ConOpen := 0;
  285.    end;
  286.  
  287.  
  288.    (* -------------------------------------------------------- *)
  289. var
  290.    e: integer;
  291.    
  292. begin
  293.  
  294. {$IFDEF DEBUGGING}
  295.    writeln('minicrt init');
  296. {$ENDIF}
  297.  
  298.    with TextRec(output) do
  299.    begin
  300.       InOutFunc := @ConOutput;
  301.       FlushFunc := @ConFlush;
  302.       OpenFunc  := @ConOpen;
  303.       BufPos := 0;
  304.    end;
  305.  
  306.    {error #18 has been reported here when operating under desqview}
  307.    {what is 18, anyway??}
  308.    assign(stdout,'');
  309.    {$i-} rewrite(stdout); {$i+}
  310.    e := ioresult;
  311.    if e <> 0 then 
  312.       writeln('[error ',e,' on stdout]');
  313. end.
  314.  
  315.